package com.beiwei.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beiwei.auto.mode.Department;
import com.beiwei.auto.mode.Job;
import com.beiwei.mode.VDepartment;
import com.beiwei.mode.VUser;
import com.beiwei.service.DepartmentService;
import com.beiwei.service.JobService;

@Controller
@RequestMapping("/department")
public class DepartmentHandler {

	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private JobService jobService;

	/**
	 * 查询该公司所有的部门
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	public String query(Model model) {
		Subject currentUser = SecurityUtils.getSubject();
		VUser user = (VUser) currentUser.getPrincipal();

		List<VDepartment> vDepartments = null;
		try {
			vDepartments = departmentService.findVDepartmentListByCompanyId(user.getUserCompanyId());

		} catch (Exception e) {
			e.printStackTrace();
		}

		model.addAttribute("vDepartments", vDepartments);
		return "departmentList";
	}

	/**
	 * 修改该部门（该部门的名称&&该部门下所有岗位）
	 */
	@RequestMapping("/edit/{departmentId}")
	@RequiresPermissions("department:update")
	public String edit(@PathVariable("departmentId") Integer departmentId, Model model) {

		Department department = null;
		List<Job> jobs = null;

		try {

			department = departmentService.findDepartmentByDepartmentId(departmentId);
			jobs = jobService.findJobListByDepartmentId(departmentId);

		} catch (Exception e) {
			e.printStackTrace();
		}

		model.addAttribute("department", department);
		model.addAttribute("jobs", jobs);

		return "jobList";
	}

	/**
	 * 修改部门名称
	 */
	@RequestMapping(value = "/editName/{departmentId}", method = RequestMethod.GET)
	@RequiresPermissions("department:update")
	public String editName(@PathVariable("departmentId") Integer departmentId, Model model) {
		Department department = null;

		try {
			department = departmentService.findDepartmentByDepartmentId(departmentId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		model.addAttribute("department", department);

		return "editDepartment";
	}

	/**
	 * 保存修改该部门（该部门的名称）
	 */
	@RequestMapping(value = "/save", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
	@RequiresPermissions("department:update")
	@ResponseBody
	public String editName(Department department) {

		try {
			if (departmentService.saveDepartment(department)) {
				return "success";
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "操作失败！";
	}

	/**
	 * 新增部门
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	@RequiresPermissions("department:create")
	public String add(Model model) {

		model.addAttribute("department", new Department());

		return "editDepartment";
	}

	/**
	 * 保存新增部门（该部门的名称）
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@RequiresPermissions("department:create")
	@ResponseBody
	public String add(Department department) {
		Subject currentUser = SecurityUtils.getSubject();
		VUser user = (VUser) currentUser.getPrincipal();
		department.setDepartmentCompanyId(user.getUserCompanyId());

		try {
			if (departmentService.addDepartment(department)) {
				return "success";
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "操作失败！";
	}
}
